package cn.ichiva.luckysheet.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;

import java.math.BigDecimal;
import java.util.*;

/**
 * TODO
 *
 * @author pzq
 * @version 1.0
 * @date 2022/1/4 14:12
 */
public class Yyhjg {
    public static void main(String[] args) {
        sort();
    }
    public static void sort() {
        String dataJson="[{\r\n" +
                "    \"author\": \"Nicholas S. Williams\",\r\n" +
                "    \"price\": 59.99,\r\n" +
                "    \"publisher\": \"John Wiley & Sons\",\r\n" +
                "    \"isbn\": \"1498591124\",\r\n" +
                "    \"title\": \"Professional Java for Web Applications\"\r\n" +
                "}, {\r\n" +
                "    \"author\": \"lml\",\r\n" +
                "    \"price\": 1000.00,\r\n" +
                "    \"publisher\": \"中华出版社\",\r\n" +
                "    \"isbn\": \"102\",\r\n" +
                "    \"title\": \"生命在于运动\"\r\n" +
                "}, {\r\n" +
                "    \"author\": \"lml\",\r\n" +
                "    \"price\": 90.00,\r\n" +
                "    \"publisher\": \"福建出版社\",\r\n" +
                "    \"isbn\": \"103\",\r\n" +
                "    \"title\": \"2020年异象\"\r\n" +
                "}, {\r\n" +
                "    \"author\": \"lml\",\r\n" +
                "    \"price\": 90.00,\r\n" +
                "    \"publisher\": \"西安出版社\",\r\n" +
                "    \"isbn\": \"108\",\r\n" +
                "    \"title\": \"以史鉴今\"\r\n" +
                "}, {\r\n" +
                "    \"author\": \"lzf\",\r\n" +
                "    \"price\": 1000.00,\r\n" +
                "    \"publisher\": \"publzf\",\r\n" +
                "    \"isbn\": \"104\",\r\n" +
                "    \"title\": \"奋斗在上海\"\r\n" +
                "}, {\r\n" +
                "    \"author\": \"lzf\",\r\n" +
                "    \"price\": 99.00,\r\n" +
                "    \"publisher\": \"中天出版社\",\r\n" +
                "    \"isbn\": \"107\",\r\n" +
                "    \"title\": \"学习方法论\"\r\n" +
                "}, {\r\n" +
                "    \"author\": \"lzf\",\r\n" +
                "    \"price\": 99.00,\r\n" +
                "    \"publisher\": \"秦皇汉武\",\r\n" +
                "    \"isbn\": \"106\",\r\n" +
                "    \"title\": \"论自力更生的必要性\"\r\n" +
                "}, {\r\n" +
                "    \"author\": \"五十\",\r\n" +
                "    \"price\": 83.00,\r\n" +
                "    \"publisher\": \"附件出版社\",\r\n" +
                "    \"isbn\": \"105\",\r\n" +
                "    \"title\": \"21世纪-中国-前进\"\r\n" +
                "}]";
        String datajson="{\n" +
                "\t\"data\": [{\n" +
                "\t\t\"amount\": \"100\",\n" +
                "\t\t\"month\": \"1\",\n" +
                "\t\t\"areaname\": \"华北\",\n" +
                "\t\t\"year\": \"2020\",\n" +
                "\t\t\"price\": \"5\",\n" +
                "\t\t\"id\": \"1\",\n" +
                "\t\t\"dept\": \"河北\",\n" +
                "\t\t\"settleamount\": \"100\"\n" +
                "\t}, {\n" +
                "\t\t\"amount\": \"202\",\n" +
                "\t\t\"month\": \"2\",\n" +
                "\t\t\"areaname\": \"华北\",\n" +
                "\t\t\"year\": \"2021\",\n" +
                "\t\t\"price\": \"6\",\n" +
                "\t\t\"id\": \"12\",\n" +
                "\t\t\"dept\": \"河南\",\n" +
                "\t\t\"settleamount\": \"202\"\n" +
                "\t}, {\n" +
                "\t\t\"amount\": \"205\",\n" +
                "\t\t\"month\": \"1\",\n" +
                "\t\t\"areaname\": \"华南\",\n" +
                "\t\t\"year\": \"2020\",\n" +
                "\t\t\"price\": \"5\",\n" +
                "\t\t\"id\": \"15\",\n" +
                "\t\t\"dept\": \"湖北\",\n" +
                "\t\t\"settleamount\": \"205\"\n" +
                "\t}, {\n" +
                "\t\t\"amount\": \"201\",\n" +
                "\t\t\"month\": \"2\",\n" +
                "\t\t\"areaname\": \"华南\",\n" +
                "\t\t\"year\": \"2021\",\n" +
                "\t\t\"price\": \"5\",\n" +
                "\t\t\"id\": \"20\",\n" +
                "\t\t\"dept\": \"长沙\",\n" +
                "\t\t\"settleamount\": \"201\"\n" +
                "\t}, {\n" +
                "\t\t\"amount\": \"200\",\n" +
                "\t\t\"month\": \"1\",\n" +
                "\t\t\"areaname\": \"华中\",\n" +
                "\t\t\"year\": \"2020\",\n" +
                "\t\t\"price\": \"5\",\n" +
                "\t\t\"id\": \"21\",\n" +
                "\t\t\"dept\": \"武汉\",\n" +
                "\t\t\"settleamount\": \"200\"\n" +
                "\t}, {\n" +
                "\t\t\"amount\": \"202\",\n" +
                "\t\t\"month\": \"2\",\n" +
                "\t\t\"areaname\": \"华中\",\n" +
                "\t\t\"year\": \"2021\",\n" +
                "\t\t\"price\": \"6\",\n" +
                "\t\t\"id\": \"22\",\n" +
                "\t\t\"dept\": \"南京\",\n" +
                "\t\t\"settleamount\": \"202\"\n" +
                "\t}]\n" +
                "}";
//        List<Map<String, Object>> dataList = JSON.parseObject(datajson,new TypeReference<List<Map<String,Object>>>(){});
        JSONObject jsonObjectStr  = JSONObject.parseObject(datajson);
        List<Map<String,Object>> dataList = (List<Map<String, Object>>) jsonObjectStr.get("data");
        System.out.println(dataList);

        List<Map<String,Object>> sortList=new ArrayList<Map<String,Object>>();
        Map<String,Object> a1=new HashMap<String,Object>();
        a1.put("code", "amount");
        a1.put("sortPosition", 3);
        a1.put("sortDirection", "desc");
        sortList.add(a1);
        Map<String,Object> a2=new HashMap<String,Object>();
        a2.put("code", "dept");
        a2.put("sortPosition", 2);
        a2.put("sortDirection", "desc");
        sortList.add(a2);
        Map<String,Object> a3=new HashMap<String,Object>();
        a3.put("code", "areaname");
        a3.put("sortPosition", 1);
        a3.put("sortDirection", "desc");
        sortList.add(a3);
//        Map<String,Object> a4=new HashMap<String,Object>();
//        a4.put("code", "title");
//        a4.put("sortPosition", 4);
//        a4.put("sortDirection", "asc");
//        sortList.add(a4);
//        Map<String,Object> a5=new HashMap<String,Object>();
//        a5.put("code", "isbn");
//        a5.put("sortPosition", 5);
//        a5.put("sortDirection", "asc");
//        sortList.add(a5);

        Collections.sort(sortList, new Comparator<Map<String, Object>>() {
            @Override
            public int compare(Map<String, Object> o1, Map<String, Object> o2) {
                // 进行判断
                int cp1 = o1.get("sortPosition").toString().compareTo(o2.get("sortPosition").toString());
                return cp1;
            }
        });

        int sortSize=sortList.size();
        int[] compareResultArr=new int[sortSize];
        int[] sortDirectionArr=new int[sortSize];
        String[] colArr=new String[sortSize];
        for(int i=0;i<sortSize;i++) {
            compareResultArr[i]=0;
            sortDirectionArr[i]=sortList.get(i).get("sortDirection").equals("asc")?1:-1;
            colArr[i]=sortList.get(i).get("code").toString();
        }


        //排序的时候，必须保证多行在custom_id之后，而单行在custom_id之前，避免数据混乱
        Collections.sort(dataList, new Comparator<Map<String, Object>>() {
            @Override
            public int compare(Map<String, Object> o1, Map<String, Object> o2) {
                // 进行判断
                for(int i=0,len=sortSize;i<len;i++) {
                    Object v1=o1.get(colArr[i]);
                    Object v2=o2.get(colArr[i]);
                    if (v1 instanceof String) {
                        compareResultArr[i]=v1.toString().compareTo(v2.toString());
                    }
                    else {
                        compareResultArr[i]=new BigDecimal(v1.toString()).compareTo(new BigDecimal(v2.toString()));
                    }

                }

                for(int i=0,len=sortSize;i<len;i++) {
                    if (compareResultArr[i]!=0) {
                        return compareResultArr[i]*sortDirectionArr[i];
                    }
                }
                //如果都是一样，则直接返回0
                return 0;
            }
        });

        System.out.println(dataList);
    }
}
